<?php
/**
 * Created by PhpStorm.
 * User: rocky
 * Date: 2022-05-24
 * Time: 21:27
 */

namespace ExAdmin\ui\component\echart;

/**
 * 漏斗图
 * Class FunnelChart
 * @package ExAdmin\ui\component\echart
 * @method static $this create(string $name) 创建
 */
class FunnelChart extends Echart
{
    protected $seriesName;
    
    public function __construct($name)
    {
        parent::__construct();
        $this->options([
            'tooltip' => [
                'trigger' => 'item',
                'formatter' => '{a} <br/>{b} : {c} ({d}%)'
            ],
            'legend' => [
                'left'=>'center'
            ],
        ]);
        $this->seriesName = $name;
    }

    /**
     * 添加数据
     * @param $name
     * @param int|\Closure $value
     * @return $this
     */
    public function data($name, $value)
    {
        if ($value instanceof \Closure) {
            $value = call_user_func($value);
        }
        $this->data[] = ['value' => $value, 'name' => $name];
        return $this;
    }

    public function jsonSerialize()
    {
        if (count($this->data) > 0) {
            $this->echart->series[] = [
                'name' => $this->seriesName,
                'type' => 'funnel',
                'left' => '5%',
                'top' => 60,
                'bottom' => 60,
                'min' => 0,
                'max' => 100,
                'minSize' => '0%',
                'maxSize' => '100%',
                'sort' => 'descending',
                'gap' => 2,

                'itemStyle' => [
                    'borderColor' => '#fff',
                    'borderWidth' => 1,
                ],
                'labelLine' => [
                    'length' => 10,
                    'lineStyle' => [
                        'width' => 1,
                        'type' => 'solid'
                    ]
                ],
                'emphasis' => [
                    'label' => [
                        'fontSize' => 20
                    ]
                ],
                'data' => $this->data,
            ];
        }
        return parent::jsonSerialize(); // TODO: Change the autogenerated stub
    }
}
